package exception_0923;

/**
 * 自定义异常
 * 注意受查和非受查
 */
public class UserLogin {
    private String userName="admin";
    private String password="123456";

    public static void main(String[] args) {
        UserLogin userLogin=new UserLogin();
        try{//调用者处理异常
            userLogin.login("admin","1234567");
        }catch (PasswordException e){
            e.printStackTrace();
        }
    }
    public void login(String name,String password) throws PasswordException {//抛出了受查异常，受查异常要处理，不然会出错
        if(!this.userName.equals(userName)){
            throw new UserNameException("用户名错误！");//不显示异常
        }
        if(!this.password.equals(password)){
            //产生异常对象之后需要显示处理
            //显示处理有两种。当前方法使用try...catch或者通过throws向上抛出
            throw new PasswordException(("密码错误！"));
        }
    }

}
//自定义异常
//用户名异常，非受查异常
class UserNameException extends RuntimeException{
    public UserNameException(String msg){
        super(msg);
    }
}
//密码异常，受查异常
class PasswordException extends Exception{
    public PasswordException (String msg){
        super(msg);
    }
}

